package cn.edu.scau.cmi.youbaiquan.controller;

import java.net.URL;
import java.util.Random;
import java.util.ResourceBundle;

import cn.edu.scau.cmi.youbaiquan.composite.consistent.ConsistentComponent;
import cn.edu.scau.cmi.youbaiquan.composite.consistent.ConsistentDepartment;
import cn.edu.scau.cmi.youbaiquan.composite.consistent.ConsistentPerson;
import cn.edu.scau.cmi.youbaiquan.composite.safe.SafeComponent;
import cn.edu.scau.cmi.youbaiquan.composite.safe.SafeDepartment;
import cn.edu.scau.cmi.youbaiquan.composite.safe.SafePerson;
import javafx.fxml.FXML;
import javafx.scene.control.*;

public class CompositeController {

    @FXML
    private ResourceBundle resources;

    @FXML
    private URL location;

    @FXML
    private TreeView<SafeComponent> safe_treeview;

    @FXML
    private TextArea safe_output;

    @FXML
    private Button safe_bt_department;

    @FXML
    private Button safe_bt_person;

    @FXML
    private Button safe_bt_sign;

    @FXML
    private TreeView<ConsistentComponent> consistent_treeview;

    @FXML
    private TextArea consistent_output;

    @FXML
    private Button consistent_bt_department;

    @FXML
    private Button consistent_bt_person;

    @FXML
    private Button consistent_bt_sign;

    Random random = new Random();

    @FXML
    void initialize() {
        assert safe_treeview != null : "fx:id=\"safe_treeview\" was not injected: check your FXML file 'CompositeUI.fxml'.";
        assert safe_output != null : "fx:id=\"safe_output\" was not injected: check your FXML file 'CompositeUI.fxml'.";
        assert safe_bt_department != null : "fx:id=\"safe_bt_department\" was not injected: check your FXML file 'CompositeUI.fxml'.";
        assert safe_bt_person != null : "fx:id=\"safe_bt_person\" was not injected: check your FXML file 'CompositeUI.fxml'.";
        assert safe_bt_sign != null : "fx:id=\"safe_bt_sign\" was not injected: check your FXML file 'CompositeUI.fxml'.";
        assert consistent_treeview != null : "fx:id=\"consistent_treeview\" was not injected: check your FXML file 'CompositeUI.fxml'.";
        assert consistent_output != null : "fx:id=\"consistent_output\" was not injected: check your FXML file 'CompositeUI.fxml'.";
        assert consistent_bt_department != null : "fx:id=\"consistent_bt_department\" was not injected: check your FXML file 'CompositeUI.fxml'.";
        assert consistent_bt_person != null : "fx:id=\"consistent_bt_person\" was not injected: check your FXML file 'CompositeUI.fxml'.";
        assert consistent_bt_sign != null : "fx:id=\"consistent_bt_sign\" was not injected: check your FXML file 'CompositeUI.fxml'.";
        
        
        ConsistentComponent organization1 = new ConsistentDepartment("总部");
        ConsistentComponent organization2 = new ConsistentDepartment("技术部");
        ConsistentComponent organization3 = new ConsistentDepartment("营销部");
        ConsistentComponent organization4 = new ConsistentDepartment("行政部");
        ConsistentComponent individual1 = new ConsistentPerson("总经理");
        ConsistentComponent individual2 = new ConsistentPerson("技术总监");
        ConsistentComponent individual3 = new ConsistentPerson("设计师");
        ConsistentComponent individual4 = new ConsistentPerson("会计");
        ConsistentComponent individual5 = new ConsistentPerson("出纳");
        ConsistentComponent individual6 = new ConsistentPerson("文秘");

        organization1.add(individual1);
        organization1.add(organization2);
        organization1.add(organization3);
        organization1.add(organization4);
        organization2.add(individual2);
        organization2.add(individual3);
        organization3.add(individual4);
        organization3.add(individual5);
        organization4.add(individual6);

        TreeItem<ConsistentComponent> root = new TreeItem<>();
        traverse(organization1, root);
        root.getChildren().get(0).setExpanded(true);
        consistent_treeview.setRoot(root.getChildren().get(0));


        SafeDepartment organization01 = new SafeDepartment("总部");
        SafeDepartment organization02 = new SafeDepartment("技术部");
        SafeDepartment organization03 = new SafeDepartment("营销部");
        SafeDepartment organization04 = new SafeDepartment("行政部");
        SafeComponent individual01 = new SafePerson("总经理");
        SafeComponent individual02 = new SafePerson("技术总监");
        SafeComponent individual03 = new SafePerson("设计师");
        SafeComponent individual04 = new SafePerson("会计");
        SafeComponent individual05 = new SafePerson("出纳");
        SafeComponent individual06 = new SafePerson("文秘");


        organization01.add(individual01);
        organization01.add(organization02);
        organization01.add(organization03);
        organization01.add(organization04);
        organization02.add(individual02);
        organization02.add(individual03);
        organization03.add(individual04);
        organization03.add(individual05);
        organization04.add(individual06);

        TreeItem<SafeComponent> root2 = new TreeItem<>();
        traverse(organization01, root2);
        root.getChildren().get(0).setExpanded(true);
        safe_treeview.setRoot(root2.getChildren().get(0));

        
    }
    
    // 添加初始化节点(一致型)
    private void traverse(ConsistentComponent component, TreeItem<ConsistentComponent> item) {
        TreeItem<ConsistentComponent> childItem = new TreeItem<>(component);
        item.getChildren().add(childItem);
        // 遍历字节点
        if (component.getChild() != null)
            for (ConsistentComponent child : component.getChild())
                traverse(child, childItem);
    }

    // 添加初始化节点(安全型)
    private void traverse(SafeComponent component, TreeItem<SafeComponent> item) {
        TreeItem<SafeComponent> childItem = new TreeItem<>(component);
        item.getChildren().add(childItem);
        if (component instanceof SafeDepartment)
            for (SafeComponent child : ((SafeDepartment) component).getChild()) {
                traverse(child, childItem);
            }
    }

    /**
     * 一致型，添加部门
     */
    @FXML
    void addDepartment_consistent(){
        TreeItem<ConsistentComponent> item = consistent_treeview.getSelectionModel().getSelectedItem();
        if(item != null && item.getValue() instanceof ConsistentDepartment){
            ConsistentComponent cc = new ConsistentDepartment("新部门" +  random.nextInt(500));
            item.getValue().getChild().add(cc);
            TreeItem<ConsistentComponent> newItem = new TreeItem<>(cc);
            item.getChildren().add(newItem);
        }else if (item == null) {
            new Alert(Alert.AlertType.INFORMATION, "Please select a node first").showAndWait();
        } else {
            new Alert(Alert.AlertType.INFORMATION, "Please select a department").showAndWait();
        }
    }

    /**
     * 一致型，添加个人
     */
    @FXML
    void addPerson_consistent(){
        TreeItem<ConsistentComponent> item = consistent_treeview.getSelectionModel().getSelectedItem();
        if(item != null && item.getValue() instanceof ConsistentDepartment){
            ConsistentComponent cc = new ConsistentPerson("新成员" +  random.nextInt(500));
            item.getValue().getChild().add(cc);
            TreeItem<ConsistentComponent> newItem = new TreeItem<>(cc);
            item.getChildren().add(newItem);
        }else if (item == null) {
            new Alert(Alert.AlertType.INFORMATION, "Please select a node first").showAndWait();
        } else {
            new Alert(Alert.AlertType.INFORMATION, "Please select a department").showAndWait();
        }
    }

    /**
     * 一致型，签到
     */
    @FXML
    public void sign_consistent() {
        TreeItem<ConsistentComponent> item = consistent_treeview.getSelectionModel().getSelectedItem();
        if (item != null) {
            traverse(item.getValue());
            consistent_output.appendText("\n-> ");
        } else {
            new Alert(Alert.AlertType.INFORMATION, "Please select a node first").showAndWait();
        }
    }

    private void traverse(ConsistentComponent component) {
        // 执行事务
        consistent_output.appendText(component.operation() + "\n");
        // 遍历字节点
        if (component.getChild() != null)
            for (ConsistentComponent child : component.getChild()){
                consistent_output.appendText("\t");
                traverse(child);
            }

    }



    /**
     * 安全型，添加部门
     */
    @FXML
    void addDepartment_safe(){
        TreeItem<SafeComponent> item = safe_treeview.getSelectionModel().getSelectedItem();
        if(item != null && item.getValue() instanceof SafeDepartment){
            SafeComponent cc = new SafeDepartment("新部门" +  random.nextInt(500));
            ((SafeDepartment) item.getValue()).getChild().add(cc);
            TreeItem<SafeComponent> newItem = new TreeItem<>(cc);
            item.getChildren().add(newItem);
        }else if (item == null) {
            new Alert(Alert.AlertType.INFORMATION, "Please select a node first").showAndWait();
        } else {
            new Alert(Alert.AlertType.INFORMATION, "Please select a department").showAndWait();
        }
    }

    /**
     * 安全型，添加个人
     */
    @FXML
    void addPerson_safe(){
        TreeItem<SafeComponent> item = safe_treeview.getSelectionModel().getSelectedItem();
        if(item != null && item.getValue() instanceof SafeDepartment){
            SafeComponent cc = new SafePerson("新成员" +  random.nextInt(500));
            ((SafeDepartment) item.getValue()).getChild().add(cc);
            TreeItem<SafeComponent> newItem = new TreeItem<>(cc);
            item.getChildren().add(newItem);
        }else if (item == null) {
            new Alert(Alert.AlertType.INFORMATION, "Please select a node first").showAndWait();
        } else {
            new Alert(Alert.AlertType.INFORMATION, "Please select a department").showAndWait();
        }
    }

    /**
     * 安全型，签到
     */
    @FXML
    public void sign_safe() {
        TreeItem<SafeComponent> item = safe_treeview.getSelectionModel().getSelectedItem();
        if (item != null) {
            traverse(item.getValue());
            safe_output.appendText("\n-> ");
        } else {
            new Alert(Alert.AlertType.INFORMATION, "Please select a node first").showAndWait();
        }
    }

    private void traverse(SafeComponent component) {
        // 执行事务
        safe_output.appendText(component.operation() + "\n");
        // 遍历字节点
        if(component instanceof SafeDepartment){
            if (((SafeDepartment) component).getChild() != null)
                for (SafeComponent child : ((SafeDepartment)component).getChild()){
                    safe_output.appendText("\t");
                    traverse(child);
                }
        }

    }


    
    
}
